package cn.ygh.bbo.framerwork.core;


import cn.ygh.bbo.common.utils.ClassUtils;
import cn.ygh.bbo.common.utils.Md5Utils;

import java.util.Date;

/**
 * 基础service
 * @author ygh
 * @Type BaseService.java
 * @Desc
 * @date 2019/11/9
 * @Version V2.0
 */
public interface BaseService {


    /**
     *创建之前公共参数处理
     */
    default void beforeCreate(Object obj){
        try {
            ClassUtils.setFieldValue(obj, "gmtCreate", new Date());
            ClassUtils.setFieldValue(obj, "gmtUpdate", new Date());
            Object password=ClassUtils.getFieldValue(obj, "password");
            if (password!=null && password.toString().length()!=32){
                String newPassword=Md5Utils.encodePassword(password.toString());
                ClassUtils.setFieldValue(obj, "password", newPassword);
            }
        } catch (IllegalAccessException ignored) {

        }
    }

    /**
     *更新之前公共参数处理
     */
    default void beforeUpdate(Object obj){
        try {
            ClassUtils.setFieldValue(obj, "gmtUpdate", new Date());
            Object password=ClassUtils.getFieldValue(obj, "password");
            if (password!=null && password.toString().length()!=32){
                String newPassword=Md5Utils.encodePassword(password.toString());
                ClassUtils.setFieldValue(obj, "password", newPassword);
            }
        } catch (IllegalAccessException ignored) {
        }
    }
}
